<html>
<head>
  <base href="done">

<script>

var navigate = true;

// TODO(gcasto): Not sure why this is necessary, but calling
// window.domAutomationController directly in setTimeout seemt to causes the
// function to be evaluated inline.
function delayedUpload() {
  window.domAutomationController.send("XHR_FINISHED");
}

function state_changed(xhr) {
  if (xhr.readyState == 4) {
    if (navigate) {
      window.top.location.href = "done.html";
    } else {
      // Pretend like auth succeeded by hiding the login and signup forms.
      document.getElementById("testform").style.display = "none";
      document.getElementById("signup_testform").style.display = "none";
      // Delay upload so that handler in PasswordAutofillAgent can be run
      // first. This will happen immediately after JS execution ends, so this
      // shouldn't introduce any timing dependent flakes.
      setTimeout(delayedUpload, 0);
    }
  }
}

function send_xhr() {
  var xhr = new XMLHttpRequest();
  xhr.onreadystatechange = function() { state_changed(xhr); };
  xhr.open("GET", "password_xhr_submit.html", true);
  xhr.send(null);
}

</script>
</head>
<body>
<form onsubmit="send_xhr(); return false;" id="testform">
  <input type="text" id="username_field" name="username_field">
  <input type="password" id="password_field" name="password_field">
  <input type="submit" id="submit_button" name="submit_button">
</form>

<form action="password_xhr_submit.html" onsubmit="send_xhr(); return false;"
      id="signup_testform">
  <input type="text" id="signup_username_field" name="signup_username_field">
  <input type="password" id="signup_password_field"
         name="signup_password_field" autocomplete="new-password">
  <input type="password" id="confirmation_password_field"
         name="confirmation_password_field" autocomplete="new-password">
  <input type="submit" id="signup_submit_button" name="signup_submit_button">
</form>
</body>
</html>
